package com.icongyou.enterprise.talent_analysis.listener;

import com.icongyou.enterprise.talent_analysis.event.StudentDataUpdateEvent;
import com.icongyou.enterprise.talent_analysis.event.TemplateChangeEvent;
import com.icongyou.enterprise.talent_analysis.service.AIInsightCacheService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

/**
 * AI洞察事件监听器测试
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("AI洞察事件监听器测试")
class AIInsightEventListenerTest {

    @Mock
    private AIInsightCacheService cacheService;

    @InjectMocks
    private AIInsightEventListener listener;

    @Test
    @DisplayName("handleStudentDataUpdate - 使学生缓存失效")
    void testHandleStudentDataUpdate_Success() {
        // Arrange
        StudentDataUpdateEvent event = new StudentDataUpdateEvent(
            this, "660e8400-e29b-41d4-a716-446655440105", 
            StudentDataUpdateEvent.UpdateType.SUBMISSION, "submission-1"
        );

        // Act
        listener.handleStudentDataUpdate(event);

        // Assert
        verify(cacheService).invalidateStudentCache("660e8400-e29b-41d4-a716-446655440105");
    }

    @Test
    @DisplayName("handleStudentDataUpdate - 异常不中断处理")
    void testHandleStudentDataUpdate_Exception() {
        // Arrange
        StudentDataUpdateEvent event = new StudentDataUpdateEvent(
            this, "660e8400-e29b-41d4-a716-446655440105",
            StudentDataUpdateEvent.UpdateType.GRADE, "grade-1"
        );
        doThrow(new RuntimeException("缓存失效失败"))
            .when(cacheService).invalidateStudentCache("660e8400-e29b-41d4-a716-446655440105");

        // Act - 不应抛出异常
        listener.handleStudentDataUpdate(event);

        // Assert
        verify(cacheService).invalidateStudentCache("660e8400-e29b-41d4-a716-446655440105");
    }

    @Test
    @DisplayName("handleTemplateChange - 批量重新生成AI洞察")
    void testHandleTemplateChange_WithStudentList() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105", "660e8400-e29b-41d4-a716-446655440103")
        );
        
        doNothing().when(cacheService).batchAsyncRegenerateInsights(
            anyList(), anyString(), anyString(), anyString()
        );

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).batchAsyncRegenerateInsights(
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105", "660e8400-e29b-41d4-a716-446655440103"),
            "old-template",
            "new-template",
            "880e8400-e29b-41d4-a716-446655440401"
        );
        verify(cacheService, never()).invalidateTemplateCache(anyString());
    }

    @Test
    @DisplayName("handleTemplateChange - 无学生列表时使旧模板缓存失效")
    void testHandleTemplateChange_NoStudentList() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Collections.emptyList()
        );
        
        doNothing().when(cacheService).invalidateTemplateCache("old-template");

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).invalidateTemplateCache("old-template");
        verify(cacheService, never()).batchAsyncRegenerateInsights(
            anyList(), anyString(), anyString(), anyString()
        );
    }

    @Test
    @DisplayName("handleTemplateChange - 旧模板为null不处理")
    void testHandleTemplateChange_NullOldTemplate() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            null, "new-template",
            Collections.emptyList()
        );

        // Act
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService, never()).invalidateTemplateCache(anyString());
        verify(cacheService, never()).batchAsyncRegenerateInsights(
            anyList(), anyString(), anyString(), anyString()
        );
    }

    @Test
    @DisplayName("handleTemplateChange - 异常不中断处理")
    void testHandleTemplateChange_Exception() {
        // Arrange
        TemplateChangeEvent event = new TemplateChangeEvent(
            this, "880e8400-e29b-41d4-a716-446655440401", "550e8400-e29b-41d4-a716-446655440003", 
            "old-template", "new-template",
            Arrays.asList("660e8400-e29b-41d4-a716-446655440105")
        );
        
        doThrow(new RuntimeException("生成失败"))
            .when(cacheService).batchAsyncRegenerateInsights(
                anyList(), anyString(), anyString(), anyString()
            );

        // Act - 不应抛出异常
        listener.handleTemplateChange(event);

        // Assert
        verify(cacheService).batchAsyncRegenerateInsights(
            anyList(), anyString(), anyString(), anyString()
        );
    }
}
